<template>
  <div style="padding: 10px; display: flex; flex-direction: column; height: 100%;">
    <el-form ref="cameraGroupFormRef" :model="form" :rules="rules" label-width="80px" style="height: calc(100% - 100px); display: flex; flex-direction: column;">
        <el-form-item label="分组名称" prop="cameraGroupName">
          <el-input v-model="form.cameraGroupName" placeholder="请输入组名称" :maxlength="30" show-word-limit/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :autosize="{ minRows: 3 }" :maxlength="120" show-word-limit placeholder="请输入备注" />
        </el-form-item>
      </el-form>

      <div style="height: 70px; display: flex; justify-content: center; align-items: end;">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="emit('close')">取 消</el-button>
      </div>
  </div>
</template>
<script lang="ts" setup>
import { cameraGroupQuery, cameraGroupForm } from '@/api/security/camera/camera-group/types';
import request from '@/utils/request';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const buttonLoading = ref(false);
const cameraGroupFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: cameraGroupForm = {
  remark: undefined,
  cameraGroupName: undefined,
  parkId: undefined
}
const data = reactive<PageData<cameraGroupForm, cameraGroupQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  rules: {
    cameraGroupName: [
      { required: true, message: '组名称不能为空', trigger: 'blur' }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

const props = defineProps({
  equipmentCameraGroupId: String | Number
});

const emit = defineEmits(['close', 'success']);

/** 获取设备组信息 */
const getCameraGroupInfo = async () => {  
  proxy?.$modal.loading('加载中...')
  const res = await request({ url: '/equipment/equipment-camera-group/' + props.equipmentCameraGroupId, method: 'get' }).finally(() => proxy?.$modal.closeLoading());
  Object.assign(form.value, res.data);
}
/** 取消按钮 */
const cancel = () => {
  reset();
  emit('close');
}

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  cameraGroupFormRef.value?.resetFields();
}

/** 提交按钮 */
const submitForm = () => {
  cameraGroupFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      await request({ url: '/equipment/equipment-camera-group', method: 'put', data: form.value }).finally(() => buttonLoading.value = false);
      proxy?.$modal.msgSuccess('操作成功');
      emit('close');
      emit('success');
      reset();
    }
  });
}

onMounted(() => {
  getCameraGroupInfo();
});
</script>
<style lang="scss" scoped>

</style>
